package com.rl.util;

import java.io.*;

public class TimeFile {
    static public int readFile() {
        String pathname = "D:\\Folders.2\\Java\\BookProject\\SSM1\\src\\main\\webapp\\text\\time.txt";
        int i = 0;
        try {
            FileReader reader = new FileReader(pathname);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                String str = line;
                i = Integer.valueOf(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }

    static public void writeFile( int i) {
        File file =new File("D:\\Folders.2\\Java\\BookProject\\SSM1\\src\\main\\webapp\\text\\time.txt");
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            File writeName = new File("D:\\Folders.2\\Java\\BookProject\\SSM1\\src\\main\\webapp\\text\\time.txt");
            // 相对路径，如果没有则要建 立一个新的output.txt文件
            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            try (FileWriter writer = new FileWriter(writeName,true);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                String s = String.valueOf(i);
                out.write(s);
                out.flush(); // 把缓存区内容压入文件
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
